import $config from '@/config'
import { cloneDeep } from 'lodash'
import axios from '@/utils/comm-request'
import urls from '@/api'
import { getToken } from '@/utils'

const state = {
  pageData: {
  },
  scenes: [],
  // 当前场景
  currSceneUUID: ''
}

const mutations = {
  /**
   * 设置当前选中场景id
   * @param {*} uuid
   */
  setCurrSceneUUID (state, uuid) {
    state.currSceneUUID = uuid
    state.currElementUUID = ''
  },
  /**
   * 跳转上一个场景
   */
  prevScene(state) {
    const pidx = getters.currSceneIndex(state)
    if (pidx > 0) {
      mutations.setCurrSceneUUID(state, state.scenes[pidx - 1].uuid)
    }
  },
  /**
   * 跳转下一个场景
   */
  nextScene(state) {
    const pidx = getters.currSceneIndex(state)
    const last = state.scenes.length - 1
    if (pidx < last) {
      mutations.setCurrSceneUUID(state, state.scenes[pidx + 1].uuid)
    }
  },
  /**
   * 设置场景
   */
  setupScene(state,numb) {
    mutations.setCurrSceneUUID(state, state.scenes[numb].uuid)
  },
  /**
   * 更新页面信息
   * @param {*} data
   */
  updatePageData(state, data) {
    state.pageData = data
  },
  /**
   * 更新场景
   * @param {*} data
   */
  updateProgramScenes(state, data) {
    state.scenes = cloneDeep(data)
  },
  /**
   * 更新节目数据
   * @param {*} data
   */
  updateProgramData(state, data) {
    mutations.updatePageData(state, {
      id: data.id,
      height: data.height ? data.height : $config.canvasPCHeight,
      width: data.width ? data.width : $config.canvasPCWidth,
      programName: data.programName ? data.programName : '默认名称',
      titlePicUrl: data.titlePicUrl ? data.titlePicUrl : 'http://192.144.132.100:8105/material/1599618080717.jpeg'
    })
    if (data.element) {
      const _pd = JSON.parse(data.element)
      if (_pd.scenes && _pd.scenes.length) {
        mutations.updateProgramScenes(state, _pd.scenes)
        mutations.setCurrSceneUUID(state, _pd.scenes[0].uuid)
      }
    }
  }
}

const actions = {
  getProgramInfo({state, commit}, id) {
    const token = getToken()
    let data = new FormData()
    data.append('token', token)
    data.append('id', id)
    axios.post(urls.getProgramInfo, data).then(res => {
      if (res && res.program && res.program.id) {
        commit('updateProgramData', res.program)
      }
    })
  }
}

const getters = {
  currSceneIndex (state) {
    if (!state.scenes || !state.scenes.length || !state.currSceneUUID) {
      return -1
    }
    return state.scenes.findIndex(v => { return v.uuid === state.currSceneUUID })
  },
  // 当前选中场景
  currScene (state) {
    // 如果不存在页面返回-1
    if (!state.scenes || !state.scenes.length || !state.currSceneUUID) {
      return { commonStyle: {}, config: {} }
    }
    return state.scenes.find(v => { return v.uuid === state.currSceneUUID })
  },
  hasCurrScene (state) {
    return getters.currSceneIndex(state) !== -1
  },
  /**
   * 背景音乐列表
   */
  bgAudioList(state) {
    if (!getters.hasCurrScene(state)) {
      return []
    }
    const scene = getters.currScene(state)
    if (!scene.propsValue || !scene.propsValue.bgAudioList || !scene.propsValue.bgAudioList.length) {
      return []
    }
    return scene.propsValue.bgAudioList
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
